import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/payfree_return_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///退货审核明细
class UiPayFreeGoodReturn {
  static var hasRackEnable = false; //是否具备整架入库功能
  static Widget build(
      BuildContext context, PayFreeReturnDetail1? data, bool isRackgoes,
      {String? title}) {
    if (StringUtils.isEmpty(data) || StringUtils.isEmpty(data?.mList)) {
      return AppUi.getEmptyView();
    }
    hasRackEnable = isRackgoes;
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).PayFreeReturnList; //退货审核明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, data?.mList, data));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(BuildContext context, List<PayFreeReturnDetail2>? data,
      PayFreeReturnDetail1? data1) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (data != null && data.length > 0) {
          var postion = 1;
          data.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells));
            if (element.isOpenAn) {
              //二级详情，标题
              children.add(TableRow(
                  decoration: BoxDecoration(
                      color: AppColors.backgroundColorWithTabelSecondary),
                  children: _titles2(context)));
              //二级详情，内容
              element.Details?.forEach((element) {
                var secondaryContents = _secondaryContents(context, element);
                children.add(TableRow(children: secondaryContents));
              });
            }
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!, data1!)));
        }
        var fixedOne = 50.0;
        if (hasRackEnable) {
          fixedOne = 180.0;
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(fixedOne),
            1: FixedColumnWidth(50),
          },
          textDirection: TextDirection.ltr,
          border: AppStyle.getTableBorder(),
          children: children,
        );
        var width = 2500.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).Operation)); //操作
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).OrderCode)); //订单编号
    children.add(_addItem(AppStrings.of(context).OrderOutCode)); //订单外部编号
    children.add(_addItem(AppStrings.of(context).ProductName2)); //货品名称
    children.add(_addItem(AppStrings.of(context).StockInCode)); //入库单号
    children.add(_addItem(AppStrings.of(context).BoxOutCode)); //箱号
    children.add(_addItem(AppStrings.of(context).ProcessName2)); //责任工序
    children.add(_addItem(AppStrings.of(context).GroupName)); //责任班组
    children.add(_addItem(AppStrings.of(context).PersonName3)); //责任人
    children.add(_addItem(AppStrings.of(context).BackDate)); //退货时间
    children.add(_addItem(AppStrings.of(context).DNum)); //数量
    children.add(_addItem(AppStrings.of(context).DArea)); //面积
    children.add(_addItem(AppStrings.of(context).DAmount)); //金额
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      PayFreeReturnDetail2 element, StateSetter setState) {
    List<Widget> children = [];
    Widget contentOne;
    if (!element.isOpenAn) {
      contentOne = InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_down,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //展开
          setState(() {
            element.isOpenAn = true;
          });
        },
      ); //操作
    } else {
      contentOne = InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_up,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //关闭
          setState(() {
            element.isOpenAn = false;
          });
        },
      ); //操作
    }
    List<Widget> containerOneChildren = [];
    containerOneChildren.add(contentOne); //操作按钮
    if (hasRackEnable) {
      var checkBoxRack = UiBase.checkBox(
          context, AppStrings.of(context).rackgoes, element.isRackgoes,
          (value) {
        element.isRackgoes = value ?? false;
      }, true);
      containerOneChildren.add(checkBoxRack); //整架入库
    }
    Container containerOne = Container(
      child: Column(
        children: containerOneChildren,
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
      ),
      alignment: Alignment.center,
    );
    children.add(Center(
      child: containerOne,
    )); //操作
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.OrderCode)); //订单编号
    children.add(_addItem(element.OrderOutCode)); //订单外部编号
    children.add(_addItem(element.ProductName)); //货品名称
    children.add(_addItem(element.StockInCode)); //入库单号
    children.add(_addItem(element.BoxOutCode)); //箱号
    children.add(_addItem(element.ProcessName,
        hint: AppStrings.of(context).ClickSelect, isBlue: true, onTap: () {
      var MsgProcess = element.MsgProcess;
      List<MsgProcessBean>? MsgProcess2 = [];
      if (MsgProcess != null) {
        MsgProcess2?.add(MsgProcessBean()); //添加一个空白的
        MsgProcess2?.addAll(MsgProcess);
      }
      var ProcessId = element.ProcessId;
      AppDialog.showDialogWithRollerPicker(context,
              datas: MsgProcess2, key: ProcessId)
          ?.then((value) {
        setState(() {
          if (value != null && element.ProcessId != value?.ProcessId) {
            //选中工序
            element.ProcessId = value?.ProcessId;
            element.ProcessName = value?.ProcessName;
            //班组清空
            element.GroupId = "";
            element.GroupName = "";
            //责任人清空
            element.PersonId = "";
            element.PersonName = "";
          }
        });
      });
    })); //责任工序
    children.add(_addItem(element.GroupName,
        hint: AppStrings.of(context).ClickSelect, isBlue: true, onTap: () {
      var MsgGroup = element.getMsgGroupWithProcess();
      if (StringUtils.isEmpty(MsgGroup)) {
        Toast.showInfo(AppStrings.of(context).SelectProcess, context); //请先选择工序
        return;
      }
      List<MsgGroupBean>? MsgGroup2 = [];
      if (MsgGroup != null) {
        MsgGroup2?.add(MsgGroupBean()); //添加一个空白的
        MsgGroup2?.addAll(MsgGroup);
      }
      var GroupId = element.GroupId;
      AppDialog.showDialogWithRollerPicker(context,
              datas: MsgGroup2, key: GroupId)
          ?.then((value) {
        setState(() {
          if (value != null && element.GroupId != value?.GroupId) {
            //选中班组
            element.GroupId = value?.GroupId;
            element.GroupName = value?.GroupName;
            //责任人清空
            element.PersonId = "";
            element.PersonName = "";
          }
        });
      });
    })); //责任班组
    children.add(_addItem(element.PersonName,
        hint: AppStrings.of(context).ClickSelect, isBlue: true, onTap: () {
      var UserInfoes = element.getMsgPersonWithGroup();
      if (StringUtils.isEmpty(UserInfoes)) {
        Toast.showInfo(AppStrings.of(context).SelectTeam, context); //请先选择班组
        return;
      }
      List<UserInfo>? UserInfoes2 = [];
      if (UserInfoes != null) {
        UserInfoes2?.add(UserInfo()); //添加一个空白的
        UserInfoes2?.addAll(UserInfoes);
      }
      var PersonId = element.PersonId;
      AppDialog.showDialogWithRollerPicker(context,
              datas: UserInfoes2, key: PersonId)
          ?.then((value) {
        setState(() {
          if (value != null && element.PersonId != value?.PersonId) {
            //选中责任人
            element.PersonId = value?.PersonId;
            element.PersonName = value?.PersonName;
          }
        });
      });
    })); //责任人
    children.add(_addItem(element.BackDate)); //退货时间
    children.add(_addItem(element.DNum)); //数量
    children.add(_addItem(element.DArea)); //面积
    children.add(_addItem(element.DAmount)); //金额
    return children;
  }

  ///二级标题
  static List<Widget> _titles2(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem(AppStrings.of(context).Width_Height)); //规格型号
    children.add(_addItem(AppStrings.of(context).ItemCode)); //安装编号
    children.add(_addItem(AppStrings.of(context).Num4)); //发货数量
    children.add(_addItem(AppStrings.of(context).ReturnNum)); //退货数量
    children.add(_addItem(AppStrings.of(context).HandleMode)); //退货分类
    children.add(_addItem(AppStrings.of(context).Area)); //面积
    children.add(_addItem(AppStrings.of(context).Price2)); //单价
    children.add(_addItem(AppStrings.of(context).Amount0)); //金额
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    return children;
  }

  ///二级内容
  static List<Widget> _secondaryContents(
    BuildContext context,
    PayFreeReturnDetail3 element,
  ) {
    List<Widget> children = [];
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem(element.Width_Height)); //规格型号
    children.add(_addItem(element.ItemCode)); //安装编号
    children.add(_addItem(element.Num)); //发货数量
    children.add(_addItem(element.ReturnNum)); //退货数量
    children.add(_addItem(element.HandleMode)); //退货分类
    children.add(_addItem(element.Area)); //面积
    children.add(_addItem(element.Price)); //单价
    children.add(_addItem(element.Amount)); //金额
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    children.add(_addItem("")); //
    return children;
  }

  ///合计
  static List<Widget> _aCombined(BuildContext context,
      List<PayFreeReturnDetail2> datas, PayFreeReturnDetail1 data) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    children.add(_addItem("")); //序号
    children.add(_addItem("")); //订单编号
    children.add(_addItem("")); //订单外部编号
    children.add(_addItem("")); //货品名称
    children.add(_addItem("")); //入库单号
    children.add(_addItem("")); //箱号
    children.add(_addItem("")); //责任工序
    children.add(_addItem("")); //责任班组
    children.add(_addItem("")); //责任人
    children.add(_addItem("")); //退货时间
    children.add(_addItem(data.TNum)); //数量
    children.add(_addItem(data.TArea)); //面积
    children.add(_addItem(data.TAmount)); //金额
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap, String? hint}) {
    Widget child = Center(child: _text(text, isBlue: isBlue, hint: hint));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false, String? hint}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    var textStr = text?.trim() ?? "";
    if (StringUtils.isEmpty(textStr) && hint != null) {
      textStr = hint.trim();
    }
    return Text(
      textStr,
      style: style,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }
}
